package io.renren.modules.sp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.redis.RedisUtils;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sp.dto.SpNoticeDTO;
import io.renren.modules.sp.entity.SpBaseMatchItemEntity;
import io.renren.modules.sp.entity.SpMatchEntity;
import io.renren.modules.sp.entity.SpMatchItemEntity;
import io.renren.modules.sp.excel.SpNoticeExcel;
import io.renren.modules.sp.service.SpBaseMatchItemService;
import io.renren.modules.sp.service.SpMatchItemService;
import io.renren.modules.sp.service.SpMatchService;
import io.renren.modules.sp.service.SpNoticeService;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
* 通知
*
* @author Mark sunlightcs@gmail.com
* @since 3.0 2023-07-26
*/
@RestController
@RequestMapping("sp/spnotice")
@Api(tags="通知")
public class SpNoticeController {
    @Autowired
    private SpNoticeService spNoticeService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("sp:spnotice:page")
    public Result<PageData<Map>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        //获取当前登录用户
        UserDetail user = (UserDetail) SecurityUtils.getSubject().getPrincipal();
        if (user.getDeptId() != null) {
            // 判断管理员的角色
            String userType = sysUserService.getSysUserType();
            String season = (String) redisUtils.get("season-" + user.getId());
            //如果是赛项管理员
            if ("2".equals(userType)) {
                String sql = " (" +
                        " mc.id in (select mi.match_id from sp_match_item mi where mi.admin_id = " + user.getId() + ")" +
                        " or (mc.season = " + season + " and match_level = 1 )" +
                        ")";
                params.put("sql", sql);
            }
            //如果是省赛管理员
            if ("3".equals(userType)) {
                //params.put("adminId", user.getId());
                params.put("sql", " (mc.admin_id = " + user.getId() + " or (mc.season = " + season + " and match_level = 1 ) )");
            }
        }
        PageData<Map> page = spNoticeService.queryPage(params);
        return new Result<PageData<Map>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("sp:spnotice:info")
    public Result<SpNoticeDTO> get(@PathVariable("id") Long id){
        SpNoticeDTO data = spNoticeService.get(id);

        return new Result<SpNoticeDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("sp:spnotice:save")
    public Result save(@RequestBody SpNoticeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        spNoticeService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("sp:spnotice:update")
    public Result update(@RequestBody SpNoticeDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        spNoticeService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("sp:spnotice:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        spNoticeService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("sp:spnotice:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<SpNoticeDTO> list = spNoticeService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "通知", list, SpNoticeExcel.class);
    }

}